<?php
/**
 * link: http://bbs.52jscn.com
 * copyright: Copyright (c) 2018 人人禾匠商城
 * author: wxf
 */

namespace app\forms\api\home_page;


use app\models\GoodsCats;
use app\models\Model;

class HomePageForm extends Model
{
    /**
     * 获取分类商品列表
     * @param $data
     * @return array
     */
    private function getCatsGoods($data)
    {
        $relationId = $data['relation_id'];
        if ($relationId > 0) {
            // 分类大于0 则查询指定分类
            $goods = $this->getThreeCatIdsGoods($relationId);
            $cat = GoodsCats::find()->where(['id' => $relationId])->select('id,name,pic_url')->one();

            $newCatGoods[] = [
                'key' => 'cat',
                'relation_id' => $cat['id'],
                'cat_pic_url' => $cat['pic_url'],
                'name' => $cat['name'],
                'list_style' => $this->mallSetting['cat_goods_cols'],
                'goods' => $goods
            ];

            return $newCatGoods;
        } else {
            // 分类ID 等于0 则查询所有分类
            $cats = GoodsCats::find()->where([
                'is_delete' => 0,
                'status' => 1,
                'mch_id' => 0,
                'mall_id' => \Yii::$app->mall->id
            ])->select('id,name,pic_url')->orderBy(['sort' => SORT_ASC])->asArray()->all();

            $newCatGoods = [];
            foreach ($cats as $cat) {
                $goods = $this->getThreeCatIdsGoods($cat['id']);
                $arr = [];
                $arr['key'] = 'cat';
                $arr['name'] = $cat['name'];
                $arr['relation_id'] = $cat['id'];
                $arr['cat_pic_url'] = $cat['pic_url'];
                $arr['goods'] = $goods;
                $arr['list_style'] = $this->mallSetting['cat_goods_cols'];
                $newCatGoods[] = $arr;
            }

            return $newCatGoods;
        }
    }

    private function getThreeCatIdsGoods($id)
    {
        $form = new CommonGoodsList();
        $form->cat_id = $id;
        $form->status = 1;
        $form->limit = $this->mallSetting['cat_goods_count'];
        $form->relations = ['goodsWarehouse'];
        $list = $form->getList();
        return $list;
    }

    /**
     * 处理图片魔方
     * @param $homePage
     * @return array|null|\yii\db\ActiveRecord
     */
    private function getBlock($homePage)
    {
        $block = HomeBlock::find()->where([
            'id' => $homePage['relation_id'],
            'is_delete' => 0
        ])->asArray()->one();

        if ($block) {
            $block['value'] = \Yii::$app->serializer->decode($block['value']);

            // 样式一
            if (count($block['value']) == 1 && $block['type'] == 1) {
                $homePage['style'] = 'height:360rpx;';
                $block['value'][0]['style'] = 'width:750rpx;height:360rpx;top:0;left:0;';
            }

            if (count($block['value']) == 2 && $block['type'] == 1) {
                $homePage['style'] = 'height:360rpx;';
                $block['value'][0]['style'] = 'width:300rpx;height:360rpx;top:0;left:0;';
                $block['value'][1]['style'] = 'width:450rpx;height:360rpx;top:0;left:300rpx;';
            }
            if (count($block['value']) == 3 && $block['type'] == 1) {
                $homePage['style'] = 'height:360rpx;';
                $block['value'][0]['style'] = 'width:300rpx;height:360rpx;top:0;left:0;';
                $block['value'][1]['style'] = 'width:450rpx;height:180rpx;top:0;left:300rpx;';
                $block['value'][2]['style'] = 'width:450rpx;height:180rpx;top:180rpx;left:300rpx;';
            }
            if (count($block['value']) == 4 && $block['type'] == 1) {
                $homePage['style'] = 'height:360rpx;';
                $block['value'][0]['style'] = 'width:300rpx;height:360rpx;top:0;left:0;';
                $block['value'][1]['style'] = 'width:450rpx;height:180rpx;top:0;left:300rpx;';
                $block['value'][2]['style'] = 'width:225rpx;height:180rpx;top:180rpx;left:300rpx;';
                $block['value'][3]['style'] = 'width:225rpx;height:180rpx;top:180rpx;left:525rpx;';
            }

            // 样式二
            if (count($block['value']) == 2 && $block['type'] == 2) {
                $homePage['style'] = 'height:240rpx;';
                $block['value'][0]['style'] = 'width:375rpx;height:240rpx;top:0;left:0;';
                $block['value'][1]['style'] = 'width:375rpx;height:240rpx;top:0;left:375rpx;';
            }
            if (count($block['value']) == 3 && $block['type'] == 2) {
                $homePage['style'] = 'height:240rpx;';
                $block['value'][0]['style'] = 'width:250rpx;height:240rpx;top:0;left:0;';
                $block['value'][1]['style'] = 'width:250rpx;height:240rpx;top:0;left:250rpx;';
                $block['value'][2]['style'] = 'width:250rpx;height:240rpx;top:0;left:500rpx;';
            }
            if (count($block['value']) == 4 && $block['type'] == 2) {
                $homePage['style'] = 'height:186rpx;';
                $block['value'][0]['style'] = 'width:186rpx;height:186rpx;top:0;left:0;';
                $block['value'][1]['style'] = 'width:186rpx;height:186rpx;top:0;left:188rpx;';
                $block['value'][2]['style'] = 'width:186rpx;height:186rpx;top:0;left:376rpx;';
                $block['value'][3]['style'] = 'width:186rpx;height:186rpx;top:0;left:564rpx;';
            }

            // 样式三
            if (count($block['value']) == 4 && $block['type'] == 3) {
                $homePage['style'] = 'height:372rpx;';
                $block['value'][0]['style'] = 'width:375rpx;height:186rpx;top:0;left:0;';
                $block['value'][1]['style'] = 'width:375rpx;height:186rpx;top:0;left:375rpx;';
                $block['value'][2]['style'] = 'width:375rpx;height:186rpx;top:186rpx;left:0;';
                $block['value'][3]['style'] = 'width:375rpx;height:186rpx;top:186rpx;left:375rpx;';
            }

            $homePage['block'] = $block;
        }
        return $homePage;
    }

    /**
     * @param $homePage
     * @param $isNew
     * @return mixed
     */
    private function getBanners($homePage, $isNew = false)
    {
        if ($this->bannerList && !$isNew) {
            $homePage['banners'] = $this->bannerList;
            return $homePage;
        }
        $banners = MallBannerRelation::find()->alias('mb')->where([
            'mb.mall_id' => \Yii::$app->mall->id,
            'mb.is_delete' => 0,
        ])->leftJoin(['b' => Banner::tableName()], 'mb.banner_id = b.id')
            ->andWhere(['b.is_delete' => 0])->select(['mb.*', 'b.title', 'b.pic_url', 'b.page_url'])->asArray()->all();
        $newData = [];
        foreach ($banners as $banner) {
            $arr = [
                'id' => $banner['banner_id'],
                'title' => $banner['title'],
                'pic_url' => $banner['pic_url'],
                'page_url' => $banner['page_url'],
            ];
            $newData[] = $arr;
        }

        $homePage['banners'] = $this->bannerList = $newData;

        return $homePage;
    }

    /**
     * @param $homePage
     * @param $isNew
     * @return mixed
     */
    private function getHomeNav($homePage, $isNew = false)
    {
        if ($this->homeNav && !$isNew) {
            $homePage['home_navs'] = $this->homeNav;
            return $homePage;
        }
        $homeNavs = HomeNav::find()->where([
            'mall_id' => \Yii::$app->mall->id,
            'status' => 1,
            'is_delete' => 0,
        ])->orderBy(['sort' => SORT_DESC])->asArray()->all();

        $newData = [];
        foreach ($homeNavs as $homeNav) {
            $arr = [
                'id' => $homeNav['id'],
                'icon_url' => $homeNav['icon_url'],
                'link_url' => $homeNav['url'],
                'name' => $homeNav['name'],
                'open_type' => $homeNav['open_type'],
                'params' => $homeNav['params'] ? \Yii::$app->serializer->decode($homeNav['params']) : [],
            ];
            $newData[] = $arr;
        }

        $homePage['home_navs'] = $this->homeNav = $newData;

        return $homePage;
    }

    /**
     * @param $homePage
     * @param $isNew
     * @return mixed
     */
    private function getTopic($homePage, $isNew = false)
    {
        if ($this->topicList && !$isNew) {
            $homePage['topics'] = $this->topicList;
            return $homePage;
        }
        $topics = Topic::find()->where([
            'mall_id' => \Yii::$app->mall->id,
            'is_delete' => 0,
        ])->orderBy(['sort' => SORT_DESC])->limit(10)->asArray()->all();

        $newData = [];
        foreach ($topics as $topic) {
            $arr = [
                'id' => $topic['id'],
                'title' => $topic['title'],
                'icon_url' => $topic['cover_pic'],
            ];
            $newData[] = $arr;
        }
        $homePage['topics'] = $this->topicList = $newData;

        return $homePage;
    }

    private function getCoupon($homePage)
    {
        $common = new CommonCouponList();
        $common->user = \Yii::$app->user->identity;
        $res = $common->getList();

        $homePage['coupons'] = $res;

        return $homePage;
    }
}
